package com.ly.ssh.service.impl;

import com.ly.ssh.Util.Page;
import com.ly.ssh.dao.PersonDao;
import com.ly.ssh.entity.Address;
import com.ly.ssh.entity.Person;
import com.ly.ssh.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;

/**
 * Created by XRom
 * On 11/16/2017.11:58 PM
 */
@Service
public class PersonServiceImpl implements PersonService {

    @Autowired(required = true)
    private PersonDao personDao;

    @Override
    public Integer savePerson() {
        Person person = new Person();
        person.setUsername("XRog");
        person.setPhone("18381005946");
        person.setRemark("this is XRog");
        Address a =new Address();
        a.setAddr("a");
        a.setPerson(person);
        return personDao.save(person);
    }
    @Transactional
    public List<Person> getAll(){
        return personDao.findAll();
    }
    public Page queryForPage(int currentPage, int pageSize) {
        // TODO Auto-generated method stub

        Page page = new Page();
        //总记录数
        int allRow = personDao.getAllRowCount();
        //当前页开始记录
        int offset = page.countOffset(currentPage,pageSize);
        //分页查询结果集
        List<Person> list = personDao.queryForPage(offset, pageSize);

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);

        return page;
    }
}
